#include "Exception.h"

#include "ElementNode.h"
#include "Font.h"



namespace NBGUI
{

	ElementNode::ElementNode(GUIDrawer* drawer, const char* name, const char* eleText, int eleType, Rect& _rect):
		elementType((GUIELEMENT)eleType),elementName(name),elementText(eleText),rect(_rect),startIndex(0),indexLen(0),
		pDrawer(drawer),hide(false)

	{
		
	}

	ElementNode::~ElementNode()
	{
		for (unsigned int i = 0; i < children.size(); ++i)
		{
			delete children[i];
		}
		 
	}

	ElementNode* ElementNode::attachElementNode( ElementNode* child )
	{
		if (!child || child->getElementType() >= HANDLER)
		{
			return nullptr;//if it is not a normal node
		}
		children.push_back(child);
		child->setParent(this);
		return child;
	}

	void ElementNode::setParent(ElementNode* p)
	{
		parent = p;
	}

	ElementNode* ElementNode::detachElementNode( ElementNode* child )
	{
		for (auto it = children.begin(); it != end(children); ++it)
		{
			if (*it == child)
			{
				return *children.erase(it);
			}
		}
		return nullptr;
	}

 
	ElementNode* ElementNode::detachElementNode( std::string elementName )
	{
		for (auto it = children.begin(); it != end(children); ++it)
		{
			if ((*it)->name() == elementName)
			{
				return *children.erase(it);
			}
		}
		return nullptr;
	}

	void ElementNode::handleEvent( Event* e )
	{
		for(auto it = children.begin(); it != children.end(); ++it)
		{
			(*it)->handleEvent(e);
		}
	}





}